import UIKit

final class AUConfig {
    
    static let shared = AUConfig()
    private init() {}

    private var _keyWindow: UIWindow?

    // 启动时设置 window
    func setupKeyWindow(_ window: UIWindow?) {
        _keyWindow = window
    }

    // MARK: - Key Window
    var keyWindow: UIWindow? {
        // 如果已设置就直接用
        if let w = _keyWindow { return w }

        // 否则动态获取
        if #available(iOS 13.0, *) {
            return UIApplication.shared.connectedScenes
                .compactMap { $0 as? UIWindowScene }
                .flatMap { $0.windows }
                .first { $0.isKeyWindow }
        } else {
            return UIApplication.shared.keyWindow
        }
    }

    // MARK: - 常用定义
    var screenWidth: CGFloat { UIScreen.main.bounds.width }
    var screenHeight: CGFloat { UIScreen.main.bounds.height }
    var statusBarHeight: CGFloat { keyWindow?.safeAreaInsets.top ?? 20 }
    var navigationBarHeight: CGFloat { 44 }
    var topBarHeight: CGFloat { statusBarHeight + navigationBarHeight }
    var tabBarHeight: CGFloat { (keyWindow?.safeAreaInsets.bottom ?? 0) > 0 ? 83 : 49 }
    var isNotchScreen: Bool { (keyWindow?.safeAreaInsets.bottom ?? 0) > 0 }
    var safeAreaTop: CGFloat { keyWindow?.safeAreaInsets.top ?? 0 }
    var safeAreaBottom: CGFloat { keyWindow?.safeAreaInsets.bottom ?? 0 }
}
